Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

389
Visualizações
I am exporting data from table convert to CSV, to JavaScript variable. . But getting Error "fputcsv() expects parameter 2 to be array" . How to do it?

This is my PHP code

<?php
require_once 'd:\xampp\htdocs\bpr\pdo_db.php';
$sql = "Select ID,FULL_NAMe,AGE from TRIAL";
$csv = get_csv_string($dbh, $sql);
echo "<script> var_csv='" . $csv . "' </script>";

function get_csv_string($dbh, $sql)
{
    try
    {
        $result = $dbh->query($sql);
        //return only the first row (we only need field names)
        $row = $result->fetch(PDO::FETCH_ASSOC);
        if ($row == null) {
            return "No Data";
        }
        $f = fopen('php://memory', 'r+');
        foreach ($row as $field => $value) {
            if (fputcsv($f, $field) === false) {
                return false;
            }
        }
        //second query gets the data
        $data = $dbh->query($sql);
        $data->setFetchMode(PDO::FETCH_ASSOC);
        foreach ($data as $row) {
            if (fputcsv($f, $row) === false) {
                return false;
            }
        } // end record loop
        rewind($f);
        $csv = stream_get_contents($f);
        return rtrim($csv);
    } catch (PDOExepction $e) {
        return $e->getMessage();
    }
    return $csv;
}

I am getting following error!

enter image description here

enter image description here

Instead of fputcsv(), I made my own code. But issue if found when I save the CSV to a javascript variable. Is there any other way to convert the data to CSV and pass it on to javascript? I will convert this csv to JSON at Client side using javascript

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're trying to output a header row to CSV with the field names.

This code fails because you're looping through the $row array and attempting to write one field header at a time.

foreach ($row as $field => $value) {
            if (fputcsv($f, $field) === false) {
                return false;
            }
        }

What you want is an array with the field names in it. You can use array_keys() for that, so your code becomes

if (fputcsv($f, array_keys($row)) === false) return false;

No loop required.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda